package com.sxbbc.api.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.EnumUtils;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.*;
import com.sxbbc.common.core.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxbbc.api.service.IHomeService;
import com.sxbbc.common.basics.service.solr.entity.Solrpage;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.CityAddressVo;
import com.sxbbc.common.core.mapper.jz.AddressMapper;
import com.sxbbc.common.core.service.system.solr.SolrproductService;
import com.sxbbc.common.core.service.system.solr.entity.SolrProductDto;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.util.CollectionUtils;

/**
 * 首页数据显示 业务处理实现类
 * 因首页数据处理比较繁杂，单独处理
 *
 * @author leehao
 * @date 2019-05-15 14:50
 */
@Service
public class HomeServiceImpl implements IHomeService {

    @Autowired
    private IAppBannerService bannerService;
    @Autowired
    private IAppIconService iconService;
    @Autowired
    private IProcMainService procMainService;
    @Autowired
    private SolrproductService solrproductService;
    @Autowired
    private IAppClassificationService appClassificationService;
    @Autowired
    private IProcThirdClassifyMainService procThirdClassifyMainService;
    @Autowired
    private IActivityDailyService activityDailyService;
    @Autowired
    private ICFootprintInfoService footprintInfoService;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private BBusinessService bBusinessService;

    @Override
    public List<AppBanner> queryBannerList() {
        BasePage<AppBanner> bannerPage = new BasePage<>();
        bannerPage.setPaging(false);
        AppBanner banner = new AppBanner();
        banner.setType(BannerPositionEnums.HOME.getId());
        banner.setOrderFlag(true);
        Page<AppBanner> page = bannerService.queryAppIconList(bannerPage, banner);
        for (AppBanner b : page.getRecords()) {
            if (!StringUtils.equals(b.getEvent(), EventEnums.SEARCH_PRODUCT.getUrl())) {
                if (StringUtils.isNotBlank(b.getUrl())) {
                    // 设置产品类型
                    ProcMain proc = procMainService.selectOneByProCode(b.getUrl());
                    if (proc != null) {
                        b.setProType(proc.getProType());
                    }
                }
            }
        }
        return page.getRecords();
    }

    @Cacheable(value = "bbcCache", key = "'queryNavIcon'")
    @Override
    public List<AppIcon> queryIconList() {
        BasePage<AppIcon> iconPage = new BasePage<>();
        iconPage.setPaging(false);
        AppIcon icon = new AppIcon();
        icon.setDisplay(StaticUtils.STATUS_YES);
        Page<AppIcon> page = iconService.queryAppIconList(iconPage, icon);
        for (AppIcon i : page.getRecords()) {
            if (!StringUtils.equals(i.getEvent(), EventEnums.SEARCH_PRODUCT.getUrl())) {
                //设置产品类型
                if (StringUtils.isNotBlank(i.getUrl())) {
                    ProcMain procMain = procMainService.selectOneByProCode(i.getUrl());
                    if (procMain != null) {
                        i.setProType(procMain.getProType());
                    }
                }
            }
        }
        return page.getRecords();
    }

    @Override
    public Solrpage<SolrProductDto> queryHomeProductList(Integer cid, Integer cpage, Integer sortType, String sortDesc) {

        SolrProductDto dto = new SolrProductDto();
        dto.setType(ProductTypeEnums.NORMAL.getId());
        if (cid != null) {
            //猜你喜欢，需要根据cid和产品分类用以区分
            QueryWrapper<CFootprintInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("cid", cid);
            int count = footprintInfoService.count(wrapper);
            //cid不为空的时候去根据浏览记录
            if (count > 0) {
                Integer secondCalssify = footprintInfoService.queryLastSecondClassify(cid);
                dto.setSclassid(secondCalssify);
            }
        }
        Solrpage<SolrProductDto> solrPage = solrproductService.queryProduct(dto, sortType, sortDesc, cpage);
        CalcuUtils calUtil = CalcuUtils.getInstance();
        ArrayList<SolrProductDto> list = new ArrayList<>();
        for (SolrProductDto s : solrPage.getList()) {
            BBusiness business = bBusinessService.getById(s.getBid());
            if (business != null) {
                s.setbTypeName(EnumUtils.getEnum(BusinessTypeEnums.class, EnumUtils.GET_ID, business.getBType()).getName());
                if (calUtil.isEquals(business.getOnOff(), BusinessScopeEnums.ONLINE.getId())) {
                    list.add(s);
                }
            }
        }
        solrPage.setList(list);
        return solrPage;
    }

    @Cacheable(value = "bbcCache", key = "'queryClassificationList'")
    @Override
    public List<AppClassification> queryClassificationList() {
        QueryWrapper<AppClassification> wrapper = new QueryWrapper<>();
        wrapper.eq("display", StaticUtils.STATUS_YES);
        List<AppClassification> list = appClassificationService.list(wrapper);
        //app -> 查出一二三级分类ID
        //小程序 -> 三级分类ID 该二级下的三级分类集合
        if (!CollectionUtils.isEmpty(list)) {
            for (AppClassification a : list) {
                if (StringUtils.equals(a.getEvent(), EventEnums.CLASSIFICATION.getUrl())) {
                    String[] classList = a.getUrl().split(",");
                    if(classList.length>0) {
                        a.setFirstClass(Integer.parseInt(classList[0]));
                    }
                    if(classList.length>1) {
                        a.setSecondClass(Integer.parseInt(classList[1]));
                    }
                    if(classList.length>2) {
                        a.setThirdClass(Integer.parseInt(classList[2]));
                    }
                    QueryWrapper<ProcThirdClassifyMain> classWrapper = new QueryWrapper<>();
                    if(classList.length>1) {
                        classWrapper.eq("second_id", Integer.parseInt(classList[1]))
                                .eq("status", StaticUtils.STATUS_YES);
                    }
                    classWrapper.orderByDesc("sort");
                    List<ProcThirdClassifyMain> thirdList = procThirdClassifyMainService.list(classWrapper);
                    a.setThirdList(thirdList);
                }
            }
        }
        return list;
    }

    @Override
    public List<CityAddressVo> queryAllCity() {
        List<CityAddressVo> list = new ArrayList<>();
        for (int i = 65; i <= 90; i++) {
            CityAddressVo vo = new CityAddressVo();
            char n = (char) i;
            String number = String.valueOf(n);
            vo.setNumber(number);
            List<Map<String, Object>> nameList = addressMapper.queryCityByNumber(number);
            vo.setList(nameList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<ActivityDaily> queryHomeDayList() {
        List<ActivityDaily> list = activityDailyService.list();
        if (!CollectionUtils.isEmpty(list)) {
            for (ActivityDaily a : list) {
                String[] classList = a.getUrl().split(",");
                a.setFirstClass(Integer.parseInt(classList[0]));
                a.setSecondClass(Integer.parseInt(classList[1]));
                QueryWrapper<ProcThirdClassifyMain> wrapper = new QueryWrapper<>();
                wrapper.eq("second_id", Integer.parseInt(classList[1])).
                        eq("status", StaticUtils.STATUS_YES).
                        orderByDesc("sort");
                List<ProcThirdClassifyMain> thirdList = procThirdClassifyMainService.list(wrapper);
                a.setThirdList(thirdList);
            }
        }
        return list;
    }
}
